//package com.bdqn.t382.filter;
//
//import jakarta.servlet.*;
//import jakarta.servlet.annotation.WebFilter;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import jakarta.servlet.http.HttpSession;
//import org.springframework.stereotype.Component;
//
//import java.io.IOException;
//
//@Component
//@WebFilter(filterName = "LoginFilter", urlPatterns = "/user/doLogin")
//public class LoginFilter implements Filter {
//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//        Filter.super.init(filterConfig);
//    }
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        HttpServletResponse response = (HttpServletResponse) servletResponse;
//
//        // 获取请求的URI
//        String uri = request.getRequestURI();
//
//        if (uri.contains("/user/doLogin")) {
//            // 获取登录参数
//            String account = request.getParameter("account");
//            String password = request.getParameter("password");
//
//            // 判断参数是否为空
//            if (account == null || account.trim().isEmpty()) {
//                // 参数为空，重定向到登录页面
//                response.sendRedirect(request.getContextPath() + "/error.jsp?msg=nullAccount");
//                return;
//            } else if (password == null || password.trim().isEmpty()) {
//                response.sendRedirect(request.getContextPath() + "/error.jsp?msg=nullPassword");
//                return;
//            }
//
//            // 参数不为空，放行请求
//            filterChain.doFilter(request, response);
//            return;
//        }
//
//        HttpSession session = request.getSession(false);
//        if (session != null && session.getAttribute("user") != null) {
//            // 用户已登录，放行请求
//            filterChain.doFilter(request, response);
//        } else {
//            // 用户未登录，重定向到登录页面
//            response.sendRedirect(request.getContextPath() + "/user/login");
//        }
//    }
//
//    @Override
//    public void destroy() {
//        Filter.super.destroy();
//    }
//}
